package vns.client.presenter;

import java.util.ArrayList;
import java.util.List;

import vns.client.VNSmartUI;
import vns.client.event.AccountEvent;
import vns.client.event.ObjectEditEvent;
import vns.client.event.WelcomeEvent;
import vns.client.helper.RPCCall;
import vns.client.service.ObjectPropertyService;
import vns.client.service.ObjectPropertyServiceAsync;
import vns.client.service.ObjectPropertyTypeService;
import vns.client.service.ObjectPropertyTypeServiceAsync;
import vns.client.service.ObjectServiceAsync;
import vns.client.service.SupperAdminServiceAsync;
import vns.client.view.ObjectUI;
import vns.client.view.SupperAdminUI;
import vns.shared.ObjectDTO;
import vns.shared.ObjectPropertyDTO;
import vns.shared.ObjectPropertyTypeDTO;
import vns.shared.ObjectTypeDTO;
import vns.shared.SupperAdminDTO;

import com.google.appengine.api.datastore.KeyFactory;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;

public class SupperAdminPresenter implements Presenter {

	@Override
	public void go(HasWidgets container) {
		container.clear();
		container.add(display.asWidget());
		dialogBox = (DialogBox) container;
		init();
	}

	public interface Display {
		Button getUpdateButton();

		Button getCancelButton();

		Button getLoginButton();

		HTML getLogOutURL();

		String getEmail();

		String getPassword();

		Widget asWidget();
	}

	private final Display display;
	private final SimpleEventBus eventBus;
	private final SupperAdminServiceAsync rpcService;
	private DialogBox dialogBox = null;
	private SupperAdminDTO adminDTO = null;
	private final String operation;

	/*
	 * Public method
	 */

	public SupperAdminPresenter(SupperAdminUI view, SimpleEventBus eventBus,
			SupperAdminServiceAsync model, SupperAdminDTO dto, String operation) {
		this.display = view;
		this.eventBus = eventBus;
		this.rpcService = model;
		this.adminDTO = dto;
		this.operation = operation;
		bind();
	}

	/*
	 * Private method
	 */

	private boolean bind() {
		display.getLoginButton().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				doLogin();
			}
		});
		display.getUpdateButton().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				doUpdate();
			}
		});
		display.getCancelButton().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				doCancel();

			}
		});
		return false;
	}

	protected void doLogin() {
		if (adminDTO.getEmail().equalsIgnoreCase(display.getEmail())
				&& adminDTO.getPass().equals(display.getPassword()))
			eventBus.fireEvent(new AccountEvent("login"));
		else {
			VNSmartUI.get().getDialogBoxSub()
					.setText("Sai mật khẩu hoặc email!");
			VNSmartUI.get().getDialogBoxSub().show();
			VNSmartUI.get().getDialogBoxSub().center();
		}
	}

	protected void doCancel() {
		if (dialogBox.isShowing() && adminDTO != null)
			dialogBox.hide();
		else {
			Window.alert("Cần tạo ít nhất một Quản trị viên");
		}
	}

	private void init() {
		dialogBox.setText("Tài khoản quản trị:");
		dialogBox.show();
		dialogBox.center();
		if (operation.equalsIgnoreCase("login")) {
			display.getUpdateButton().setVisible(false);
			display.getCancelButton().setVisible(false);
			display.getLogOutURL().setHTML("<a href='"+
					VNSmartUI.get().getAccountData().getGoogleLogoutUrl()+
					"'>[Thoát]</a>");
		} else {
			display.getLoginButton().setVisible(false);
		}
	}

	private void doUpdate() {
		adminDTO = new SupperAdminDTO();
		adminDTO.setEmail(display.getEmail());
		adminDTO.setPass(display.getPassword());
		new RPCCall<SupperAdminDTO>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Cập nhật thất bại! Đã có lỗi xảy ra! \n "
						+ caught.toString());

			}

			@Override
			public void onSuccess(SupperAdminDTO result) {
				Window.alert("Cập nhật thành công!");
				eventBus.fireEvent(new WelcomeEvent());
			}

			@Override
			protected void callService(AsyncCallback<SupperAdminDTO> cb) {
				rpcService.Update(adminDTO, cb);
			}
		}.retry(3);
	}
}
